import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { FormBuilder, Validators } from '@angular/forms';
import { ApiService } from '../../../core/api/api.service';
import { MessageService } from '../../../core/service/message.service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';

/**
 * 企业管理-新增
 */

@Component({
    templateUrl: './add.html',
})
export class QYAddComponent implements OnInit {

    validateForm;
    typeList = [{ name: '国有企业', id: 'StateOwned' }, { name: '民企', id: 'PrivateOwned' }, { name: '其它', id: 'Other' }]
    areaList = [];
    collectorList = [];
    // "cityName": "南通市",             // 城市名称 (固定)
    // "areaName": "港闸区",            // 市区编号  (getNextLevel.do 获取)


    ngOnInit() {
        this.validateForm = this.fb.group({
            name: [null, [Validators.required]],
            code: [null, [Validators.required]],
            natureType: [null, [Validators.required]],
            legalPerson: [null, [Validators.required]],
            address: [null, [Validators.required]],
            zipcode: [null, [Validators.required]],
            securityDirector: [null, [Validators.required]],
            contactEmail: [null, [Validators.required]],
            contactTel: [null, [Validators.required]],
            collectorId: [null, [Validators.required]],
        });
        this.apiService.post('采集器编号列表', {}).then((res: any) => {
           this.collectorList = res.data.list;
        })
    }
    // 更换城市
    onChangeCity(cityId) {
        this.apiService.post('获取各个区', {
            "provinceId": "",
            "cityId": cityId,
            "areaId": ""
        }).then((res: any) => {
            this.areaList = res.data;
        })
    }
    // 提交
    submit(data) {
        data.cityName = "南通市";
        data.cityId = "2";	// 市编号
        data.areaId = "3";// 地区编号
        data.provinceId = "1";// 地区编号
        data.areaName = "南通市";
        data.provinceName = "江苏省";// 地区编号
        if (this.validateForm.valid) {
            this.apiService.post('创建企业', data).then((res: any) => {
                this.areaList = res.data;
                this.router.navigate(['/qy/list']);
            })
        } else {
            this.nzMessageService.error('请正确填写表单后提交')
        }
    }
    constructor(
        private router: Router,
        private fb: FormBuilder,
        private apiService: ApiService,
        private message: MessageService,
        private nzMessageService: NzMessageService,
        private modal: NzModalService
    ) { }
}
